<?php
include_once($_SERVER['DOCUMENT_ROOT']."ExiaStore/Model/BDD.php");
include($_SERVER['DOCUMENT_ROOT']."ExiaStore/Model/Users/adresse.php");
include_once($_SERVER['DOCUMENT_ROOT']."ExiaStore/Model/Security/securite.php");
         
class membre { //création d'une nouvelle classe 
    private $login;
    private $pass;
    private $email;
    private $nom;
    private $prenom;
    private $adresse;
    private $bdd;
    private $securite;
    
    
    public function __construct(){ //constructeur de la classe membre
        $this->securite = new securite();
        $this->bdd = new BDD();   
    }
    
    public function getMembreInfo(){
        $req = "SELECT * FROM membre WHERE login =".$_SESSION['login'];
        $rslt = $this->bdd->getBDD()->prepare($req);
        $tab = $rslt->fetch(PDO::FETCH_ASSOC);
        $this->login = $tab['login'];
        $this->pass = $tab['pass'];
        $this->email = $tab['email'];
        $this->nom = $tab['nom'];
        $this->prenom = $tab['prenom'];
        
        $req = "SELECT * FROM adresse INNER JOIN localiser ON localiser.idAdresse = adresse.idAdresse INNER JOIN "
                . "ville ON ville.ville = localiser.ville WHERE adresse.idAdresse =".$tab['idAdresse'];
        $rslt = $this->bdd->getBDD()->prepare($req);
        $tab = $rslt->fetch(PDO::FETCH_ASSOC);
        $this->adresse = new adresse(false);
        $this->adresse->recupAdresse($tab['adresse'],  $tab['ville'], $tab['codepostal'], $tab['pays']);
    }
    
    public function initNewUser(){
        $this->login = $this->securite->antixss($_POST['login']);
        $this->pass = $this->securite->hachagemdp($this->securite->antixss($_POST['pass']));
        $this->email = $this->securite->antixss($_POST['email']);
        $this->nom = $this->securite->antixss($_POST['nom']);
        $this->prenom = $this->securite->antixss($_POST['prenom']);
        $this->adresse = new adresse(true); //appelle un nouvel objet adresse
        $this->adduser();
    }
        
    public function adduser(){ //Ajoute les informations de l'utilisateur dans la BDD
        $this->adresse->addToBDD(); //Ajoute les informations liées à l'adresse de l'utilisateur dans la BDD
        $req = "INSERT INTO membre (login, motDePasse, idadresse, email, nom, prenom) VALUES (:login, :motDePasse, :idadresse, :email, :nom, :prenom)";
        $rslt = $this->bdd->getBDD()->prepare($req);
        $rslt->execute(array(":login" => $this->login, ":motDePasse" => $this->pass, 
        ":idadresse" => $this->adresse->getIDAdresse(), ":email" => $this->email, ":nom" => $this->nom , ":prenom"=> $this->prenom));     
    }
    
    public function getLogin(){//getter de login
        return $this->login;
    }
    
    public function modifuser(){ //Ajoute les informations de l'utilisateur dans la BDD
        $req = $this->bdd->getBDD()->prepare('UPDATE membre SET nom = :nom, prenom = :prenom, email = :email WHERE login=:login');
        $req->execute(array(
	'nom' => $nom= $_POST['nom'],
        'prenom' => $prenom= $_POST['prenom'],
        'email' => $email=$_POST['email'],
        'login' => $login=$_SESSION['login'],
	));   
    }
    
}

?>
